package org.example.controller;

import org.example.pojo.Headline;
import org.example.service.HeadlineService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("headline")
@CrossOrigin
public class HeadLineController {

    @Autowired
    private HeadlineService headlineService;


    /**
     * 发布新闻头条
     * 需要用户登录后才能访问
     *
     * @param headline 新闻头条对象
     * @param token 用户认证令牌
     * @return 发布结果
     */
    @PostMapping("publish")
    public Result publish(@RequestBody  Headline headline, @RequestHeader String token){
        Result result = headlineService.publish(headline,token);
        return result;
    }


    /**
     * 根据ID查找新闻头条详情
     *
     * @param hid 新闻头条ID
     * @return 包含新闻头条信息的结果对象
     */
    @PostMapping("findHeadlineByHid")
    public Result findHeadlineByHid(Integer hid){
        Headline headline = headlineService.getById(hid);
        Map data = new HashMap();
        data.put("headline",headline);
        return Result.ok(data);
    }


    /**
     * 更新新闻头条信息
     *
     * @param headline 更新后的新闻头条对象
     * @return 更新结果
     */
    @PostMapping("update")
    public Result update(@RequestBody Headline headline){
        Result result = headlineService.updateData(headline);
        return result;
    }

    /**
     * 根据ID删除新闻头条
     *
     * @param hid 要删除的新闻头条ID
     * @return 删除结果
     */
    @PostMapping("removeByHid")
    public Result removeByHid(Integer hid){
        headlineService.removeById(hid);
        return Result.ok(null);
    }
}